import {ajaxFun} from "../../utils/ajax";
const app=getApp()//获取小程序实例
Page({

  data: {
   phone:"17638475974",
   code:"",
   disabled:false,
   text:"获取文本",
   time:10,
   timer:null,
   code:"",
   getcode:""
  },
 async getcodeFun(){
      let  phone=this.data.phone;

      let reg=/^1[3,4,5,6,7,8,9]\d{9}$/
      if(!reg.test(phone)){
         wx.showToast({
           title: '手机号错误',
           icon:"none"
         })

         return false;
      }
      // ajax
      let result=await ajaxFun("/sendCode","post",{phone:Number(this.data.phone)},"application/x-www-form-urlencoded")
   
      this.data.timer=setInterval(()=>{
           if(this.data.time<=0){
               clearInterval(this.data.timer);
               this.setData({
                text:"获取文本",
                time:10,
                disabled:false
               })
               return false
           }
           this.data.time--;

           this.setData({
               text:this.data.time,
               disabled:true
           })
       },1000)
  },
 async loginBtn(){
    let  phone=this.data.phone;

    let reg=/^1[3,4,5,6,7,8,9]\d{9}$/
    if(!reg.test(phone)){
       wx.showToast({
         title: '手机号错误',
         icon:"none"
       })

       return false;
    }
    if(this.data.code==""){
        wx.showToast({
          title: '验证码不能为空',
          icon:"none"
        })

        return false
    }
     let result=await ajaxFun("/login/mobile","post",{
      phone:Number(this.data.phone),
      captcha:Number(this.data.code)
     })
    wx.setStorageSync('token', result.data.token);

    this.setData({//双向绑定 setData()  react
       phone:"",
       code:""
    })
    wx.switchTab({
      url: '/pages/my/my',
    })
  },

  longinwechat(){//微信授权登录
    wx.login({
      success: (res) => {
        console.log(res.code);
         ajaxFun(`/wechat/authorize/program/login?code=1234567`,"post")
      },
    })
    
     
  },
  onLoad(){
     wx.login({
       success: (res) => {
          console.log(res.code)
          this.setData({
            getcode:res.code
          })
       },
     })
  }
  
})